package org.duoyi.striver.service.task;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.duoyi.striver.StriverApplication;
import org.duoyi.striver.consts.Area;
import org.duoyi.striver.consts.Picture;
import org.duoyi.striver.consts.Shortcuts;
import org.duoyi.striver.model.Point;
import org.duoyi.striver.model.impl.Sleeper;
import org.duoyi.striver.service.common.CommonService;

@Slf4j
public class BuddyTask extends AbstractTask {

    @Override
    public void doTask() {
        if (!before()) {
            return;
        }
        int counter = 0;
        while (true) {
            if (counter % 2 == 0 && isFinished()) {
                break;
            }

            Sleeper.sleep(2000);

            synchronized (StriverApplication.class) {
                if (Picture.伙伴任务_百晓通对话标识.find()) {
                    log.info("百晓通处领取任务");
                    Area.伙伴任务_百晓通领取.leftClick();
                    Point point = Picture.伙伴任务_150点任务.findPoint();
                    if (point != null) {
                        log.info("领取150点奖励任务");
                        point.addR(RandomUtil.randomInt(10, 30), RandomUtil.randomInt(10, 20)).leftClick();
                    } else {
                        point = Picture.伙伴任务_50点任务.findPoint();
                        if (point != null) {
                            log.info("领取50点奖励任务");
                            point.addR(RandomUtil.randomInt(10, 30), RandomUtil.randomInt(10, 20)).leftClick();
                        }
                    }
                }
            }

            CommonService.isNpcTaking();

            isTaking();

            isGiving();

            //小型对话后面大概率是战斗
            isMiniTakingForFight();

            CommonService.isFighting();

            if (CommonService.isBoothStore()) {
                continue;
            }

            if (CommonService.isPetStore()) {
                continue;
            }

            if (CommonService.isSystemStore()) {
                continue;
            }

            Sleeper.sleep();
            if (counter == 9) {
                counter = 0;
            } else {
                counter++;
            }
        }
    }

    private boolean isMiniTakingForFight() {
        if (!Picture.伙伴任务_小型战斗对话.find()) {
            return false;
        }
        log.info("NPC对话进入战斗");
        Area.伙伴任务_小型战斗对话_第一项.leftClick();
        return true;
    }

    private boolean isGiving() {
//        if (!Picture.伙伴任务_给予物品.find()) {
//            return false;
//        }
//        Area.伙伴任务_给予物品.leftClick();
        return true;
    }

    private boolean isTaking() {
        synchronized (StriverApplication.class) {
            boolean flag = false;
            for (int i = 0; i < 5; i++) {
                flag = Picture.伙伴任务_对话.find();
                if (flag) {
                    break;
                }
                Sleeper.sleep();
            }
            if (!flag) {
                return false;
            }
            log.info("伙伴任务剧情对话");
            Area.伙伴任务_对话.leftClick();
            int times = 0;
            while (times++ < 20) {
                if (Picture.伙伴任务_对话.find()) {
                    log.info("伙伴任务剧情对话");
                    Area.伙伴任务_对话.leftClick();
                    times = 0;
                    continue;
                }
                Sleeper.sleep(100);
            }
            return true;
        }
    }

    private boolean isFinished() {
        synchronized (StriverApplication.class) {
            Shortcuts.任务面板.press();
            Point point = Picture.伙伴任务_任务面板_选中.findPoint();
            if (point == null) {
                point = Picture.伙伴任务_任务面板_未选中.findPoint();
                if (point != null) {
                    point.leftClick();
                }
            }
            if (point == null) {
                CommonService.openDailyPanel();
                point = Picture.伙伴任务_未完成.findPoint();
                if (point == null) {
                    return true;
                }
                point.addR(RandomUtil.randomInt(10, 30), RandomUtil.randomInt(80, 90)).leftClick();
            } else {
                Area.任务面板_立即传送.leftClick();
            }
            return false;
        }
    }

    private boolean before() {
        synchronized (StriverApplication.class) {
            CommonService.openDailyPanel();

            Point point = Picture.伙伴任务_未完成.findPoint();
            if (point == null) {
                return false;
            }
            point.addR(RandomUtil.randomInt(10, 30), RandomUtil.randomInt(80, 90)).leftClick();

            Sleeper.sleep(2000);
            if (Picture.伙伴任务_百晓通对话标识.find()) {
                Area.伙伴任务_百晓通领取.leftClick();
            }
            Sleeper.sleep();
            point = Picture.伙伴任务_150点任务.findPoint();
            if (point != null) {
                log.info("领取150点奖励任务");
                point.addR(RandomUtil.randomInt(10, 30), RandomUtil.randomInt(10, 20)).leftClick();
            } else {
                point = Picture.伙伴任务_50点任务.findPoint();
                if (point != null) {
                    log.info("领取50点奖励任务");
                    point.addR(RandomUtil.randomInt(10, 30), RandomUtil.randomInt(10, 20)).leftClick();
                }
            }
            return true;
        }
    }
}
